package com.xphuong.newskios;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.xphuong.adapter.TopicListAdapter;
import com.xphuong.newskios.util.Util;

public class TopicActivity extends Activity implements OnItemClickListener {

	private String[] topicNames;
	private String[] topicLinks;
	private TopicListAdapter mAdapter;

	private TextView mTitle;
	private ListView mTopicLst;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_topic);

		Intent intent = getIntent();
		int newsPaperId = intent.getIntExtra(Util.NEWSPAPER_ID, -1);
		String newsPaperTitle = intent.getStringExtra(Util.NEWSPAPER_TITLE);

		if (newsPaperId > -1) {
			topicNames = Util.getTopicName(this, newsPaperId);
			topicLinks = Util.getTopicLink(this, newsPaperId);

			if (topicNames != null && topicNames.length > 0
					&& topicLinks != null && topicLinks.length > 0) {
				mAdapter = new TopicListAdapter(this, topicNames, topicLinks);
			}
		}

		mTitle = (TextView) findViewById(R.id.title_tv);
		mTopicLst = (ListView) findViewById(R.id.topic_list);

		Typeface typeFace = Typeface.createFromAsset(getAssets(),
				"RobotoLight.ttf");

		mTitle.setTypeface(typeFace);
		mTitle.setText(newsPaperTitle);

		mTopicLst.setAdapter(mAdapter);
		mTopicLst.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		// TODO Auto-generated method stub
		if (Util.isNetworkAvaiable(this)) {
			Intent i = new Intent(this, NewsListActivity.class);
			i.putExtra(Util.TOPIC_LINK, topicLinks[position]);
			startActivity(i);
		}
	}
}
